package cn.lyd.pet.controller;

import cn.lyd.base.jwt.UserInfo;
import cn.lyd.base.until.*;
import cn.lyd.org.domain.Shop;
import cn.lyd.org.service.IShopService;
import cn.lyd.pet.domain.PetDetail;
import cn.lyd.pet.service.IPetService;
import cn.lyd.pet.domain.Pet;
import cn.lyd.pet.query.PetQuery;
import cn.lyd.user.domain.Logininfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/pet")
public class PetController {
    @Autowired
    public IPetService petService;
    @Autowired
    public IShopService shopService;


    /**
     * 保存和修改公用的
     *
     * @param pet 传递的实体
     * @return AjaxResult转换结果
     */
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody Pet pet) {
        try {
            if (pet.getId() != null)
                petService.update(pet);
            else
                petService.add(pet);
            return new AjaxResult();
        } catch (BussinessException e) {
            return new AjaxResult(false, e.getMessage());
        }catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false, "操作失败");
        }
    }

    /**
     * 删除对象信息
     *
     * @param id
     * @return
     */
    @DeleteMapping(value = "/{id}")
    public AjaxResult delete(@PathVariable("id") Long id) {
        try {
            petService.delete(id);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false, "操作失败");
        }
    }

    //获取宠物
    @GetMapping("/{id}")
    public Pet get(@PathVariable("id") Long id) {
        return petService.findById(id);
    }

    //获取宠物详情展示给前端
    @GetMapping("/show/{id}")
    public Pet show(@PathVariable("id") Long id) {
        return petService.findShowById(id);
    }


    /**
     * 查看所有的宠物信息
     *
     * @return
     */
    @GetMapping()
    public List<Pet> list() {

        return petService.findAll();
    }


    /**
     * 分页查询数据
     *
     * @param query 查询对象
     * @return PageList 分页对象
     */
    @PostMapping()
    public PageList<Pet> json(@RequestBody PetQuery query ) {
        //如果本地线程存放的当前登录人的信息+菜单权限不为空
        //UserInfo userInfo = ThreadLocalUtil.getUserInfo();
        if (ThreadLocalUtil.getUserInfo()!=null) {
            //登录人的shop不为空===这个人目前是店铺入驻来的==店长
            if(ThreadLocalUtil.getUserInfo().getLogininfo().getShop()!=null){
                //获取他对应的店铺id----有店铺id==店长或员工   -----没有店铺id===后台管理人员
                Logininfo logininfo = ThreadLocalUtil.getUserInfo().getLogininfo();
                Shop shop = logininfo.getShop();
                //通过query传给sql
                query.setShop_id(shop.getId());
            }
        }
        return petService.queryByPage(query);
    }

    /**
     * 通过宠物id查询
     * @param id    宠物id
     * @return
     */
    @GetMapping("/detail/{id}")
    public Pet detail(@PathVariable("id") Long id) {
        return petService.findDetailById(id);
    }


    /**
     * 宠物上架
     * @param ids
     * @return
     */
    @PostMapping("/onsale")
    public AjaxResult onsale(@RequestBody List<Long> ids ) {
        try {
            petService.onsale(ids);
            return AjaxResult.me();
        } catch (BussinessException e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMsg(e.getMessage());
        }catch (Exception e){
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMsg("系统繁忙，请稍后再试！");
        }
    }


    /**
     * 宠物下架
     * @param ids
     * @return
     */
    @PostMapping("/offsale")
    public AjaxResult offsale(@RequestBody List<Long> ids ) {
        try {
            petService.offsale(ids);
            return AjaxResult.me();
        } catch (BussinessException e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMsg(e.getMessage());
        }catch (Exception e){
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMsg("系统繁忙，请稍后再试！");
        }
    }




}
